package com.example.administrator.timecount;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeCountService extends Service {
    private String TAG="TimeCountService";
    String SVAE_DIR = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "TimeCount";
    String SVAE_PATH =SVAE_DIR+File.separator + "BatteryInfo.txt";
    private BatteryReceiver receiver;
    private int level=0;

    public TimeCountService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        init();
    }
    private void init() {
        Log.e(TAG, "init: " );
        receiver=new BatteryReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(receiver,filter);
    }
    class BatteryReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            int result=intent.getIntExtra("level",0);
            if(result!=level){
                level=result;
                String dateinfo=date()+": "+level+"%"+"\n\r";
                Log.e(TAG, "level: "+level);
                Log.e(TAG, "dateinfo: "+dateinfo);
                saveDateInfo(dateinfo);
            }
        }
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private String date(){
        long time=System.currentTimeMillis();//long now = android.os.SystemClock.uptimeMillis();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d1=new Date(time);
        String t1=format.format(d1);
        return t1;
    }
    private void  saveDateInfo(String  batteryInfo){
        File fileDir=new File(SVAE_DIR);
        File file=new File(SVAE_PATH);
        if(!file.exists()){
            if(!fileDir.exists()){
                fileDir.mkdirs();
            }
            try {
                Log.i(TAG, "saveDateInfo: "+file);
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        FileWriter writer = null;
        try {
            // 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
            writer = new FileWriter(SVAE_PATH, true);
            writer.write(batteryInfo.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(writer != null){
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
